<?php
require_once('Kbs/Controller/Action.php');

class BlogController extends Kbs_Controller_Action 
{
    static $caching = false;

    public function init()
    {
        $this->view->css()->append('geshiPhp.css')
                          ->append('geshiHtml.css');

        /* Should be open in production !!!!!!!!
         * Should be open in production !!!!!!!!
         * Should be open in production !!!!!!!!
         
        $frontendOptions = array(
           'lifetime' => 7200, 
           //'debug_header' => true,
           'regexps' => array(
                            '^/' => array('cache' => false),
                            '^/blog/' => array('cache' => false),
                            '^/blog/index' => array('cache' => false),
                            '^/blog/list' => array('cache' => true),
                            '^/blog/list/category/1' => array('cache' => true),
                       ),
           'default_options' => array(
                'cache_with_get_variables' => true,
                'cache_with_post_variables' => true,
                'make_id_with_cookie_variables' => true,
                'cache_with_session_variables' => true,
                'cache_with_files_variables' => true,
                'cache_with_cookie_variables' => true,
            )
        );

        $backendOptions = array(
            'cache_dir' => PROJECT_ROOT . '/repository/cache/' 
        );

        $cache = Zend_Cache::factory('Page', 
                                     'File', 
                                     $frontendOptions, 
                                     $backendOptions);
        $cache->start();
      */
    }

    public function indexAction() 
    {
        $this->_forward('list');
    }

    public function listAction()
    {
        if ($this->getRequest()->isGet()) {
            $this->view->categoryId = $this->_getParam('category');
            $this->view->postId = $this->_getParam('post');
        }

        if (self::$caching) {
            $cache = Kbs::initCache('Postlist');
            $id = $cache->id . Kbs::initLocale();
            $html = $cache->load($id);
            if(false == $html) {
                $html = $this->getPostslist($this->view->categoryId, $this->view->postId);
                $cache->save($html, $id, $cache->tags);
            }
        } else {
            $html = $this->getPostslist($this->view->categoryId, $this->view->postId);
        }

        $this->view->list = $html;
    }

    public function searchAction()
    {
        /*
        if ($this->getRequest()->isPost()) {
            $searchTags = $this->_getParam('searchTags');
            $tags = preg_split('/[\s]+/', trim($searchTags));

            if (!empty($tags)) {
                foreach ($tags as $tag) {
                }
            }
        }*/
        $this->_forward('list');
    }

    /**
     * To get the post(s)
     */
    public function getPostslist($categoryId = null, $postId = null)
    {
        $postObj = new Kbs_Model_Business_Post();
        $posts = $postObj->getPostsListByLocale($categoryId, $postId);

        $html = '';

        if (!empty($posts)) {
            $paginator = Zend_Paginator::factory($posts);
            $page = $this->_getParam('page');
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage(5);

            if (count($paginator)) {
                $this->view->posts = $paginator;
                $pc = $this->view->paginationControl($paginator, 'Sliding', 'blog/paginationControl.phtml');

                $html .= $pc
                       . $this->view->render('blog/post.phtml')
                       . $pc;
            }
            // if in post page
            if (isset($postId)) {
                $html .= $this->view->render('blog/comments.phtml');
            }
        }

        return $html;
    }
}
